package com.dinstar.dinlink.ui;

import android.os.Bundle;
import org.linphone.core.Call;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.dinstar.dinlink.databinding.ActivityCallConnectBinding;
import com.dinstar.linphone.BaseActivity;
import com.dinstar.linphone.core.LinphoneManager;


public class CallConnectActivity extends BaseActivity {

    private ActivityCallConnectBinding callConnectBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        callConnectBinding = ActivityCallConnectBinding.inflate(getLayoutInflater());
        setContentView(callConnectBinding.getRoot());
        setCurrentActivity(this);
        initView();
    }

    private void initView() {
        Call currentCall = LinphoneManager.getInstance().getCurrentCall();
        if (LinphoneManager.getInstance().isVideoCall(currentCall)){
            callConnectBinding.videoFrameView.setVisibility(View.VISIBLE);
            LinphoneManager.getInstance().setVideoWindowId(
                    callConnectBinding.localPreview,
                    callConnectBinding.nativeVideoTextureView
                    );
        }
        String displayName = currentCall.getRemoteAddress().getDisplayName();
        callConnectBinding.callDisplayName.setText(displayName);
        Glide.with(this)
                .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201712%2F10%2F20171210134053_rMszF.thumb.700_0.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1722393070&t=0d7923c0909e4fc04b71fadceb54440f")
                .placeholder(com.dinstar.linphone.R.drawable.ic_launcher_foreground)
                .apply(new RequestOptions().transform(new RoundedCorners(10)))
                .into(callConnectBinding.callHeader);

        callConnectBinding.callConnectTime.start();

        boolean defaultMicEnable = LinphoneManager.getInstance().micEnabled();
        callConnectBinding.microEnable.setSelected(defaultMicEnable);
        if (defaultMicEnable){
            callConnectBinding.microEnableText.setText("麦克风");
        } else {
            callConnectBinding.microEnableText.setText("麦克风已关");
        }
        callConnectBinding.microEnable.setOnClickListener(v -> {
            boolean micEnable = !LinphoneManager.getInstance().micEnabled();
            if (micEnable){
                callConnectBinding.microEnable.setSelected(true);
                callConnectBinding.microEnableText.setText("麦克风");
            } else {
                callConnectBinding.microEnable.setSelected(false);
                callConnectBinding.microEnableText.setText("麦克风已关");
            }
            LinphoneManager.getInstance().enableMic(micEnable);
        });


        boolean defaultSpeakerEnable = LinphoneManager.getInstance().speakerEnabled();
        callConnectBinding.speakerEnable.setSelected(defaultSpeakerEnable);
        if (defaultSpeakerEnable){
            callConnectBinding.speakerEnableText.setText("扬声器已开");
        } else {
            if (LinphoneManager.getInstance().isBluetoothAudioRouteCurrentlyUsed()){
                callConnectBinding.speakerEnableText.setText("蓝牙");
            } else {
                callConnectBinding.speakerEnableText.setText("扬声器");
            }
        }
        callConnectBinding.speakerEnable.setOnClickListener(v -> {
            boolean speakerEnable = !LinphoneManager.getInstance().speakerEnabled();
            if (speakerEnable){
                callConnectBinding.speakerEnable.setSelected(true);
                callConnectBinding.speakerEnableText.setText("扬声器已开");
            } else {
                if (LinphoneManager.getInstance().isBluetoothAudioRouteAvailable()){
                    callConnectBinding.speakerEnable.setSelected(false);
                    callConnectBinding.speakerEnableText.setText("蓝牙");
                } else {
                    callConnectBinding.speakerEnable.setSelected(false);
                    callConnectBinding.speakerEnableText.setText("扬声器");
                }
            }
            LinphoneManager.getInstance().enableSpeaker(speakerEnable);
        });

        callConnectBinding.hangUp.setOnClickListener(v -> {
            callConnectBinding.callConnectTime.stop();
            int state = LinphoneManager.getInstance().terminateCall(currentCall);
            if (state == 0) {
                finish();
            } else {
                Toast.makeText(this, "挂断失败", Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeCurrentActivity(this);
    }
}
